use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use strum::Display;

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Display)]
pub enum MysqlColType {
    Unknown,
    TinyInt { unsigned: bool },
    SmallInt { unsigned: bool },
    MediumInt { unsigned: bool },
    Int { unsigned: bool },
    BigInt { unsigned: bool },
    Float,
    Double,
    Decimal { precision: u32, scale: u32 },
    Time,
    Date,
    DateTime,
    // timezone diff with utc in seconds
    // refer: https://dev.mysql.com/doc/refman/8.0/en/datetime.html
    Timestamp { timezone_offset: i64 },
    Year,
    // for char(length), the maximum length is 255,
    // for varchar(length), the maximum length is 65535
    // refer: https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html
    Char { length: u64, charset: String },
    Varchar { length: u64, charset: String },
    TinyText { length: u64, charset: String },
    MediumText { length: u64, charset: String },
    Text { length: u64, charset: String },
    LongText { length: u64, charset: String },
    Binary { length: u8 },
    VarBinary { length: u16 },
    TinyBlob,
    MediumBlob,
    LongBlob,
    Blob,
    Bit,
    Set { items: HashMap<u64, String> },
    Enum { items: Vec<String> },
    Json,
}

impl MysqlColType {
    pub fn is_string(&self) -> bool {
        matches!(
            self,
            Self::Char { .. }
                | Self::Varchar { .. }
                | Self::TinyText { .. }
                | Self::MediumText { .. }
                | Self::Text { .. }
                | Self::LongText { .. }
        )
    }
}
